<cfcomponent displayname="Snip Controller" extends="ModelGlue.unity.controller.Controller" output="false">
	<!--- 
		Any function set up to listen for the onRequestStart message happens before any of the <event-handlers>.
		This is a good place to put things like session defaults.
	--->
	<cffunction name="init" access="public" returntype="snipcontroller" output="false">
		<cfargument name="ModelGlue" required="true" type="ModelGlue.ModelGlue" />
		<cfargument name="instanceName" required="true" type="string" />
		<cfset var modelPath = "snipex.model" />
		<cfset var facadePath = "snipex.facades" />
		
		<cfset super.Init(arguments.ModelGlue) />
		<cfset variables.userFacade = createobject("component", facadePath & ".userFacade") />
		<cfset variables.snipService = getModelGlue().getConfigBean("snipService") />
		<cfset variables.libAccessService = getModelGlue().getConfigBean("libAccessService") />
		<cfset variables.reactor = getModelGlue().getConfigBean("ormService") />
		<cfset variables.udfs = createobject("component", modelPath & ".udfs") />

		<cfreturn this>		
	</cffunction>

	<cffunction name="getSnip" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfif isNumeric(arguments.event.getValue('snipID'))>
			<cfset arguments.event.setValue('snip', variables.snipService.getByID(arguments.event.getValue('snipID'))) />
		<cfelse>
			<cfset arguments.event.setValue('snip', variables.snipService.getByID(0)) />
		</cfif>
	</cffunction>
	
	<cffunction name="checkSnipIDAccess" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset var qSnip = false />
		<!--- <cfif isNumeric(arguments.event.getValue('snipID')) and 
				(not (arguments.event.getValue('roles'),'sysadmin')  or not listfindnocase(arguments.event.getValue('roles'),'admin') )>
			<cfset qSnip = variables.snipService.checkSnipAccess(arguments.event.getValue('snipID')) />
			<cfif qSnip.recordcount is 0>
			
			</cfif>
		</cfif> --->
	</cffunction>
	
	<cffunction name="getSnipsByLibID" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset arguments.event.setValue('snips',
				variables.snipService.getSnipsByLibID(
					libid=iif(isnumeric(arguments.event.getValue('libID')),de(arguments.event.getValue('libID')),0),
					userID=variables.userFacade.getUserID()))/>
		
	</cffunction>
	
	<cffunction name="deleteSnip" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>	
		
		<cfset variables.snipService.remove(arguments.event.getValue('snip'))/>
	</cffunction>
	
	<cffunction name="saveSnip" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset var snipBean = false />
		<cfif not isNumeric(arguments.event.getValue('snipID')) >
			<cfset snipBean = variables.snipService.getByID(0) />
			<cfset snipBean.setcreated(now()) />
		<cfelse>
			<cfset snipBean = variables.snipService.getByID(arguments.event.getValue('snipID')) />
		</cfif>
		<cfif arguments.event.valueExists('name')>
			<cfset snipBean.setname(arguments.event.getValue('name')) />
		</cfif>
		<cfif arguments.event.valueExists('useremail')>
			<cfset snipBean.setuseremail(arguments.event.getValue('useremail')) />
		</cfif>
		<cfif arguments.event.valueExists('parent')>
			<cfset snipBean.setparent(arguments.event.getValue('parent')) />
		</cfif>
		<cfif arguments.event.valueExists('help')>
			<cfset snipBean.sethelp(arguments.event.getValue('help')) />
		</cfif>
		<cfif arguments.event.valueExists('description')>
			<cfset snipBean.setdescription(arguments.event.getValue('description')) />
		</cfif>
		<cfif arguments.event.valueExists('starttext')>
			<cfset snipBean.setstarttext(arguments.event.getValue('starttext')) />
		</cfif>
		<cfif arguments.event.valueExists('endtext')>
			<cfset snipBean.setendtext(arguments.event.getValue('endtext')) />
		</cfif>
		<cfif arguments.event.valueExists('author')>
			<cfset snipBean.setauthor(arguments.event.getValue('author')) />
		</cfif>
		<cfif arguments.event.valueExists('platforms')>
			<cfset snipBean.setplatforms(arguments.event.getValue('platforms')) />
		</cfif>
		<cfif arguments.event.valueExists('template')>
			<cfset snipBean.settemplate(arguments.event.getValue('template')) />
		</cfif>
		
		<cfif arguments.event.valueExists('published')>
			<cfset snipBean.setpublished(arguments.event.getValue('published')) />
		</cfif>

		<cfif arguments.event.valueExists('userID')>
			<cfif arguments.event.getValue('userID') is not variables.userFacade.getUserID() 
					and (listfindnocase(arguments.event.getValue('roles'),'sysadmin')  or listfindnocase(arguments.event.getValue('roles'),'admin') )>
				<cfset snipBean.setOwnerID(arguments.event.getValue('userID')) />
			<cfelse>
				<cfset snipBean.setOwnerID(variables.userFacade.getUserID()) />
			</cfif>
		<cfelse>
			<cfset snipBean.setOwnerID(variables.userFacade.getUserID()) />
		</cfif>
		
		<cfset snipBean.setupdated(now()) />
		<cfset variables.snipService.save(snipBean) />
		<cfset arguments.event.setValue('libID',parent) />
	</cffunction>
	
	<cffunction name="downloadSnip" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
	</cffunction>
	
	<cffunction name="searchSnips" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
	</cffunction>
	
</cfcomponent>